﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'rightFindByUser')
	BEGIN
		DROP  Procedure  rightFindByUser
	END
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[rightFindByUser]
	-- Add the parameters for the stored procedure here
	@usr_name VARCHAR(50), 
	@app_id INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @usr_id INT;

	SELECT @usr_id = lit_usuario_id
	FROM lit_usuario
	WHERE lit_usuario_username = @usr_name
	AND lit_aplicacion_id = @app_id;

	IF (@usr_id IS NOT NULL) BEGIN
		SELECT DISTINCT p.*
		FROM lit_privilegio p
		JOIN lit_rol_privilegio rp ON p.lit_privilegio_id = rp.lit_privilegio_id
		JOIN lit_rol r ON r.lit_rol_id = rp.lit_rol_id
		JOIN lit_usuario_rol ur ON r.lit_rol_id = ur.lit_rol_id
		WHERE ur.lit_usuario_id = @usr_id;
	
	END

  END
GO